/**
 * Created by user on 2015/12/24.
 */

var async = require('async');
var mysqlUtil = require('../../../common/utils/mysqlUtil');
var toolUtil = require('../../../common/utils/toolUtil');
exports.scoreShopListService = function(req ,callback) {

    var body = req.body;
    var page = req.params.page;
    var size = Number(req.params.size);
    var index = (Number(req.params.page) - 1) * size;
    var totalPage = 0;


    var sql1 = " select count(*) as count from score_shop where 1=1";
    var sql2 = " select * from score_shop where 1=1 ";

    if (body.name) {
        sql1 += " and product_name like '%" + body.name + "%'";
        sql2 += "and product_name like '%" + body.name +"%'";
    }
    sql2 += " order by create_time desc limit " + index +"," + size

    logger.debug(sql1);
    logger.debug(sql2);

    async.series([
        function (callbackInner) {
            mysqlUtil.query(sql1, function (err, result) {
                if (err || result.length == 0) {
                    callbackInner(err, null);
                }else {
                    var count = result[0].count;
                    var temp = {count: count, totalPage:Math.ceil(count/size)};
                    callbackInner(err, temp);
                }
            });
        },
        function (callbackInner) {

            mysqlUtil.query(sql2, function (err, result) {
                callbackInner(err, result);
            });
        }
    ], callback);

};
